package com.qf.controller;

import com.qf.service.OrderStatisticService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName OrderStatistic
 * @Date: 2025/5/1 10:37
 * @Description: 描述
 */

@RestController
@RequestMapping("/statistic")
public class OrderStatistic {

    @Autowired
    private OrderStatisticService orderStatisticService;

    /**
     * 日销售额统计
     * @return
     */
    @GetMapping("/dailySales")
    public R dailySales(){
        List<Map<String,Object>> list = orderStatisticService.dailySales();
        // 创建一个长度为31的数组，默认值为0
        ArrayList<String> sales_day = new ArrayList<>(31);
        for (int i = 0; i <= 30; i++) {
            sales_day.add("0");
        }

        for (Map<String,Object> map : list) {
            String day_temp = map.get("date").toString().substring(8,10);
            Integer day = Integer.parseInt(day_temp);
            sales_day.set(day,map.get("daily_sales").toString());
        }
        return R.ok(sales_day);
    }


    /**
     * 月销售额统计
     * @return
     */
    @GetMapping("/monthlySales")
    public R monthlySales(){
        List<Map<String,Object>> list = orderStatisticService.monthlySales();
        // 创建一个长度为12的数组，默认值为0
        ArrayList<String> sales_month = new ArrayList<>(12);
        for (int i = 0; i <= 11; i++) {
            sales_month.add("0");
        }

        for (Map<String,Object> map : list) {
            String month_temp = map.get("month").toString().substring(5,7);
            Integer month = Integer.parseInt(month_temp);
            sales_month.set(month-1,map.get("monthly_sales").toString());
        }
        return R.ok(sales_month);
    }
}
